"use strict";

import React from 'react';
import RootSiblings from 'react-native-root-siblings'
import PopoverContainer from './PopoverContainer';

let sibling = undefined
class Popover extends React.Component {

    static displayName = 'Popover';
    static propTypes = PopoverContainer.propTypes;

    static show = (options) => {

        sibling = new RootSiblings(
            <PopoverContainer
                {...options}
                isVisible={true}
            />
        );
        return sibling;
    };

    static hide = () => {
        if (sibling instanceof RootSiblings) {
            sibling.destroy();
        } else {
            console.warn(`Popover.hide expected a \`RootSiblings\` instance as argument.\nBut got \`${typeof sibling}\` instead.`);
        }
    };

    _popover = null;

    constructor(props) {
        super(props);
    };

    componentDidMount = () => {
        this._popover = new RootSiblings(
            <PopoverContainer
                 {...this.props}
            />
        );
    };


    componentDidUpdate(){
        this._popover.update(
            <PopoverContainer
                    {...this.props}
            />
        );
    }

    componentWillUnmount = () => {
        this._popover.destroy();
    };

    render(){
        return null;
    }

}

module.exports = Popover;
